from dataclasses import dataclass
import time
from datetime import datetime
import logging

from high_rotary_evaporation_os.StatusEnums import *
# Configure logging
logging.basicConfig(level=logging.INFO,
                    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)


# ======= Global Variables =======
@dataclass
class PeakInfo:
    peak_id: int
    sample_id: int
    time: datetime
    small_bottle_id: int


class GlobalVariables:
    def __init__(self):
        # Bottle capacities
        self.big_bottle_v = 1000  # ml
        self.big_bottle_current_v = 0  # Current volume
        self.small_bottle_current_v = 0  # Current volume
        self.small_bottle_current_status = []
        self.big_bottle_current_status = []

        # IDs
        self.big_bottle_id = [1, 2]
        self.small_bottle_id = [1, 2, 3, 4, 5, 6, 7, 8, 9]
        self.column_id = [1, 2, 3, 4, 5, 6]
        self.sample_id = [1, 2, 3, 4, 5, 6, 7, 8, 9]
        self.chromatograph_module_id = [1, 2]

        # Solvent volumes
        self.ethyl_acetate_v = 5000  # ml
        self.petroleum_ether_v = 5000  # ml
        self.sample_v = 10  # ml

        # Peak information
        self.peak_info = []

        # Status of components
        self.component_status = {
            "chromatograph_module_status": ChromatographModuleStatus.IDLE,
            "collection_rack_status": CollectionRackStatus.IDLE,
            "rotary_evaporation_status": RotaryEvaporationStatus.IDLE,
            "washing_rack_status": WashingRackStatus.IDLE_1,
            "column_rack_status": ColumnRackStatus.IDLE,
            "abb_status": AbbStatus.IDLE,
            "scara_status": ScaraStatus.IDLE,
            "big_bottle_status": BigBottleStatus.IDLE_RACK,
            "small_bottle_status": SmallBottleStatus.IDLE_RACK,
            "ethyl_acetate_status": EthylAcetateStatus.SUFFICIENT,
            "petroleum_ether_status": PetroleumEtherStatus.SUFFICIENT,
            "sample_status": SampleStatus.SUFFICIENT
        }

    def update_status(self, component: str, status: int) -> None:
        """Update the status of a component"""
        self.component_status[component] = status
        logger.info(f"Updated {component} to {status}")

    def get_status(self, component: str) -> int:
        """Get the status of a component"""
        return self.component_status[component]

    def add_peak_info(self, peak_id: int, sample_id: int, small_bottle_id: int) -> None:
        """Add peak information"""
        peak_info = PeakInfo(
            peak_id=peak_id,
            sample_id=sample_id,
            time=datetime.now(),
            small_bottle_id=small_bottle_id
        )
        self.peak_info.append(peak_info)
        logger.info(f"Added peak info: {peak_info}")
